Shader "Custom/FlagMixer"
{
    Properties
    {
        [HDR]_BaseColor("BaseColor",Color) = (1,1,1,1)
        _LightIntensity("LightIntensity",float) = 1
        _BaseMap("Albedo", 2D) = "white" {}
        _Mask("Mask", 2D) = "white" {}
        _CampColorIntensity("Camp Color Intensity", Range( 0 , 10)) = 3
        [HDR]_CampColor("Camp Color", Color) = (1,1,1,1)
        _FlagColorIntensity("Flag Color Intensity", Range( 0 , 10)) = 3
        [HDR]_FlagColor("Flag Color", Color) = (1,1,1,1)
        _BadgeColor("BadgeColor", Color) = (0.5,0.5,0.5,1)
        _BadgeTexture("Badge Texture" , 2D) = "white" {}
        _Count("Count",int) = 1
    }
    SubShader
    {
        Tags
        {
            "RenderType" = "Opaque" "Queue" = "Geometry" "RenderPipeline" = "UniversalPipeline"
        }

        Pass
        {
            Tags
            {
                "LightMode" = "UniversalForward"
            }
            Cull Off
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag


            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"

            CBUFFER_START(UnityPerMaterial)
            half4 _BaseColor;
            float _LightIntensity;
            float4 _CampColor;
            float4 _Mask_ST;
            float4 _FlagColor;
            float _CampColorIntensity;
            float _FlagColorIntensity;
            float4 _SpecColor;
            float _SpecIntensity;
            float _Smoothness;
            float4 _BadgeColor;
            float4 _BadgeTexture_ST;
            int _Count;
            CBUFFER_END

            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BaseMap);
            TEXTURE2D(_Mask);
            SAMPLER(sampler_Mask);
            TEXTURE2D(_BadgeTexture);
            SAMPLER(sampler_BadgeTexture);

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 positionCS : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.uv = v.uv;
                o.positionCS = TransformObjectToHClip(v.vertex);
                return o;
            }

            half4 frag(v2f i) : SV_Target
            {
                half4 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv.xy) * _BaseColor;

                float2 off = float2(0, 0);
                if (_Count == 2) off.y = 0.5;

                float2 f2 = i.uv.xy * _BadgeTexture_ST.xy + _BadgeTexture_ST.zw + off;

                float badge = SAMPLE_TEXTURE2D(_BadgeTexture, sampler_BadgeTexture, f2).a;

                if (_Count == 1)
                {
                    if (f2.y < 1 || f2.y > 2) badge = 0;
                }
                else if (_Count == 2)
                {
                    if (f2.y < 1 || f2.y > 3) badge = 0;
                }

                _FlagColor = FastSRGBToLinear(_FlagColor);
                float2 uv_Mask = i.uv.xy * _Mask_ST.xy + _Mask_ST.zw;
                // float4 mask = SAMPLE_TEXTURE2D(_Mask, sampler_Mask, uv_Mask);
                // float4 campCol = lerp(baseColor, (baseColor * _CampColor * _CampColorIntensity), mask.r);
                // float4 flagCol = lerp(campCol, (baseColor * _FlagColor * _FlagColorIntensity), mask.g);
                half3 diffuseColor = baseColor.rgb * _FlagColor;
                // diffuseColor = diffuseColor * _LightIntensity;
                half3 finalColor = diffuseColor.rgb;

                finalColor = lerp(finalColor, 0.15, badge);
                // finalColor = saturate(finalColor + 0.2 * badge);

                return half4(finalColor, 1);
            }
            ENDHLSL
        }
    }
}
